// index.js
// 获取应用实例

const Encrypt = require('../../utils/jsencrypt.js'); //路径看个人的放哪里哈
let cryptFirst = new Encrypt.JSEncrypt();
cryptFirst.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCKRFad1RDxZ0FhPWZUuOZLl1wT8EsUgLYbL8Y/4F+JAudQexHX7ZRJUpbSKOvb2hfQwjRLiAKNQ+Pl5y1i3VLRXdYbwoveB6whL5OomwPkyj0oceR88HZLCM+3PFiiWkI25ncf7Y8KQPSqdELDHaklXrrXjNxZhd9ikPKSsX6m0QIDAQAB");
var hasClick = false;
var token = "";
var staffNo = "";
var pointId = 495;
var wifiName = "huatu-sso";
var timestamp = 0;

Page({
  data: {
    inputUsernameValue: '',
    inputPasswordValue: '',
    inputMacValue: '0e:05:88:0a:f8:f2',
    remembertPassword: true,
    version: '2=2.3.4',
    uuid : '',
    longitude : '',
    latitude : '',
  },
  onLoad() {
    debugger
    this.setData({
      inputUsernameValue: wx.getStorageSync('Username'),
      inputPasswordValue: wx.getStorageSync('Password'),
      version: wx.getStorageSync('Version'),
      uuid : wx.getStorageSync('uuid'),
      longitude : wx.getStorageSync('longitude'),
      latitude : wx.getStorageSync('latitude'),
      remembertPassword: true,
    })
  },
  getSign() {
    timestamp = parseInt(new Date().getTime() / 1000)
    var str = "#" + pointId + timestamp + latitude + this.data.inputMacValue + longitude + "#";
    var md5 = require('../../utils/md5.js')
    return md5.md5(str)
  },
  getPassword() {
    return cryptFirst.encrypt(this.data.inputPasswordValue)
  },
  clockIn() {
    var that = this
    var sign = this.getSign()
    wx.request({
      url: 'http://oa.huatu.com/clockIn/clockIn.json',
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'token': token,
        'version': that.data.version
      },
      data: {
        pointId: pointId,
        wifiName: wifiName,
        longitude: that.data.longitude,
        latitude: that.data.latitude,
        staffNo: staffNo,
        macAddress: that.data.inputMacValue,
        sign: sign,
        timestamp: timestamp,
        // 8B991508-2C39-42CD-9FB5-1E70F15153CC
        mobileMacAddress: that.data.uuid
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.showToast({
            title: '打卡成功'
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '系统错误',
          icon: 'error'
        })
      },
      complete: function (res) {
        wx.hideLoading()
        hasClick = false
      }
    })
  },
  clickMe: function () {
    var that = this
    if (hasClick) {
      return
    }
    if (this.data.remembertPassword) {
      wx.setStorage({
        key: "Username",
        data: this.data.inputUsernameValue
      })
      wx.setStorage({
        key: "Password",
        data: this.data.inputPasswordValue
      })
      wx.setStorage({
        key: "Version",
        data: this.data.version
      })
      wx.setStorage({
        key: "uuid",
        data: this.data.uuid
      })
      wx.setStorage({
        key: "longitude",
        data: this.data.longitude
      })
      wx.setStorage({
        key: "latitude",
        data: this.data.latitude
      })
    }
    hasClick = true
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'http://oa.huatu.com/login.json',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        userName: that.data.inputUsernameValue,
        accountType: 1,
        password: that.getPassword()
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          token = res.data.data.token
          staffNo = res.data.data.staffNo
          that.clockIn()
        } else {
          wx.hideLoading();
          hasClick = false
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        }

      },
      fail: function (res) {
        wx.hideLoading();
        hasClick = false
        wx.showToast({
          title: '系统错误',
          icon: 'error'
        })
      },

    })

  },
  bindUsernameInput: function (e) {
    this.setData({
      inputUsernameValue: e.detail.value
    })
  },
  bindPasswordInput: function (e) {
    this.setData({
      inputPasswordValue: e.detail.value
    })
  },
  bindMacInput: function (e) {
    this.setData({
      inputMacValue: e.detail.value
    })
  },
  bindVersionInput: function (e) {
    this.setData({
      version: e.detail.value
    })
  },
  bindUuidInput: function (e) {
    this.setData({
      uuid: e.detail.value
    })
  },
  bindLongitudeInput: function (e) {
    this.setData({
      longitude: e.detail.value
    })
  },
  bindLatitudeInput: function (e) {
    this.setData({
      latitude: e.detail.value
    })
  }
})